//
//  HNSH_AccountHeaderView.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/2/28.
//

#import "HNSH_AccountHeaderView.h"

@interface HNSH_AccountHeaderView ()
/* headerImgV */
@property (nonatomic, strong) UIImageView *headerImgV;
/* nameLabel */
@property (nonatomic, strong) UILabel *nameLabel;
/* 邀请码 */
@property (nonatomic, strong) UILabel *yqmLabel;
/* copyButton */
@property (nonatomic, strong) UIButton *cyButton;
@end

@implementation HNSH_AccountHeaderView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self configUI];
    }
    return self;
}

- (void)setUserInfo:(ST_UserInfo *)userInfo {
    [self.headerImgV sd_setImageWithURL:[NSURL URLWithString:userInfo.avatar_url] placeholderImage:[UIImage imageNamed:@"base_user_header_icon"]];
    self.nameLabel.text = userInfo.nickname;
    self.yqmLabel.text = [NSString stringWithFormat:@"邀请码：%@",userInfo.invite_code];
}

- (void)configUI {
    [self addSubview:self.headerImgV];
    [self addSubview:self.nameLabel];
    
    [self addSubview:self.cyButton];
    
    [self.headerImgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.mas_centerY);
        make.left.mas_equalTo(16);
        make.width.height.mas_equalTo(48);
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headerImgV.mas_right).offset(12);
        make.top.equalTo(self.headerImgV.mas_top).offset(2);
        make.height.mas_equalTo(22);
    }];
    
    if ([[NSUserDefaults standardUserDefaults] boolForKey:KUserShowHiddenContentID]) {
        [self addSubview:self.yqmLabel];
        [self.yqmLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.headerImgV.mas_right).offset(12);
            make.bottom.equalTo(self.headerImgV.mas_bottom).offset(-2);
            make.height.mas_equalTo(17);
        }];
        
    }
    
    [self.cyButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-16);
        make.centerY.equalTo(self.headerImgV.mas_centerY);
        make.width.mas_equalTo(68);
        make.height.mas_equalTo(32);
    }];
    WeakSelf(weakSelf);
    [[self.cyButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        if (weakSelf.changeBlock) {
            weakSelf.changeBlock();
        }
    }];
    
    UIView *lineView = [[UIView alloc]init];
    lineView.backgroundColor = [UIColor colorWithHexString:@"#F2F3F6"];
    [self addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(-0);
        make.bottom.mas_equalTo(0);
        make.height.mas_equalTo(1);
    }];
}

#pragma mark lazy
- (UIImageView *)headerImgV {
    if (!_headerImgV) {
        _headerImgV = [[UIImageView alloc]init];
        _headerImgV.contentMode = UIViewContentModeScaleAspectFill;
        _headerImgV.layer.cornerRadius = 24;
        _headerImgV.layer.masksToBounds = YES;
    }
    return _headerImgV;
}

- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc]init];
        _nameLabel.text = @"";
        _nameLabel.textColor = [UIColor appBlackLabelColor];
        _nameLabel.font = [UIFont boldSystemFontOfSize:16];
    }
    return _nameLabel;
}

- (UILabel *)yqmLabel {
    if (!_yqmLabel) {
        _yqmLabel = [[UILabel alloc]init];
        _yqmLabel.text = @"";
        _yqmLabel.textColor = [UIColor appGrayLabelColor];
        _yqmLabel.font = [UIFont systemFontOfSize:12];
    }
    return _yqmLabel;
}

- (UIButton *)cyButton {
    if (!_cyButton) {
        _cyButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cyButton setTitle:@"编辑" forState:UIControlStateNormal];
        [_cyButton setTitleColor:[UIColor appBlackLabelColor] forState:UIControlStateNormal];
        _cyButton.titleLabel.font = [UIFont boldSystemFontOfSize:14];
        _cyButton.layer.cornerRadius = 16.0f;
        _cyButton.layer.masksToBounds = YES;
        _cyButton.layer.borderColor = [[UIColor colorWithHexString:@"#E4E4E4"] CGColor];
        _cyButton.layer.borderWidth = 1.0f;
    }
    return _cyButton;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
